<template>
  <div class="script-detail-page" v-if="script">
    <header class="detail-header">
      <div class="back-button" @click="$router.back()">‹ 返回</div>
      <h1>剧本详情</h1>
    </header>
    <main class="detail-content">
      <img :src="script.coverImage" alt="剧本封面" class="cover-image-large"/>
      <h2 class="script-title">{{ script.name }}</h2>
      <div class="info-pills">
        <span class="pill">{{ script.level }}</span>
        <span class="pill">{{ formatSupportCount(script.supportCount) }}</span>
        <span class="pill">{{ script.duration }}h</span>
      </div>
      <div class="tag-pills">
        <span v-for="tag in script.tagName" :key="tag" class="tag-pill">{{ tag }}</span>
      </div>
      <div class="description-box">
        <h3>剧本简介</h3>
        <div v-html="script.description"></div>
      </div>
    </main>
  </div>
  <div v-else-if="loading" class="loading-indicator">
    加载中...
  </div>
  <div v-else class="error-indicator">
    加载失败或未找到该剧本。
  </div>
</template>

<script>
import {getUserScriptsByIdService} from '@/api/user.js';

export default {
  name: 'ScriptDetail',
  props: ['id'],
  data() {
    return {
      script: null,
      loading: true,
    };
  },
  methods: {
    async fetchScriptDetail() {
      this.loading = true;
      try {
        const res = await getUserScriptsByIdService(this.id);
        this.script = res.data;
      } catch (error) {
        console.error("获取剧本详情失败:", error);
        this.script = null;
      } finally {
        this.loading = false;
      }
    },
    formatSupportCount(counts) {
      if (!counts || counts.length === 0) return '';
      if (counts.length === 1) return `${counts[0]}人`;
      const min = Math.min(...counts);
      const max = Math.max(...counts);
      return `${min}-${max}人`;
    }
  },
  created() {
    this.fetchScriptDetail();
  },
};
</script>

<style scoped>
.script-detail-page {
  padding: 20px 16px;
  background-color: #f9f9f9;
  font-size: 16px;
}

.detail-header {
  display: flex;
  align-items: center;
  position: relative;
  text-align: center;
  margin-bottom: 24px;
}

.back-button {
  position: absolute;
  left: 0;
  font-size: 20px;
  color: #555;
  cursor: pointer;
  padding: 8px 12px;
  border-radius: 8px;
  transition: background-color 0.2s;
}

.back-button:hover {
  background-color: rgba(0, 0, 0, 0.05);
}

.detail-header h1 {
  flex-grow: 1;
  margin: 0;
  font-size: 20px;
}

.cover-image-large {
  width: 100%;
  max-width: 280px;
  display: block;
  margin: 0 auto 24px;
  border-radius: 12px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
}

.script-title {
  text-align: center;
  font-size: 24px;
  margin-bottom: 18px;
  line-height: 1.3;
}

.info-pills, .tag-pills {
  display: flex;
  justify-content: center;
  flex-wrap: wrap;
  gap: 12px;
  margin-bottom: 18px;
}

.pill {
  background-color: #e0e0e0;
  color: #333;
  padding: 8px 16px;
  border-radius: 18px;
  font-size: 15px;
  min-height: 44px;
  display: flex;
  align-items: center;
  justify-content: center;
}

.tag-pill {
  background-color: #ffc107;
  color: white;
  padding: 8px 16px;
  border-radius: 18px;
  font-size: 15px;
  min-height: 44px;
  display: flex;
  align-items: center;
  justify-content: center;
}

.description-box {
  background-color: #fff;
  padding: 20px;
  border-radius: 12px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.05);
  line-height: 1.6;
  font-size: 16px;
}

.description-box h3 {
  margin-top: 0;
  border-bottom: 2px solid #ffc107;
  padding-bottom: 12px;
  margin-bottom: 12px;
  font-size: 18px;
}

/* 富文本内容样式 */
.description-box :deep(p) {
  margin: 0 0 16px 0;
  line-height: 1.6;
}

.description-box :deep(p:last-child) {
  margin-bottom: 0;
}

.description-box :deep(img) {
  max-width: 100% !important;
  height: auto !important;
  border-radius: 8px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  margin: 12px 0;
  display: block;
}

.description-box :deep(h1),
.description-box :deep(h2),
.description-box :deep(h3),
.description-box :deep(h4),
.description-box :deep(h5),
.description-box :deep(h6) {
  margin: 20px 0 12px 0;
  color: #333;
  font-weight: 600;
  line-height: 1.4;
}

.description-box :deep(h1) { font-size: 20px; }
.description-box :deep(h2) { font-size: 18px; }
.description-box :deep(h3) { font-size: 16px; }
.description-box :deep(h4) { font-size: 15px; }
.description-box :deep(h5) { font-size: 14px; }
.description-box :deep(h6) { font-size: 13px; }

.description-box :deep(ul),
.description-box :deep(ol) {
  margin: 12px 0;
  padding-left: 20px;
}

.description-box :deep(li) {
  margin: 6px 0;
  line-height: 1.6;
}

.description-box :deep(blockquote) {
  margin: 16px 0;
  padding: 12px 16px;
  border-left: 4px solid #ffc107;
  background-color: #f8f9fa;
  color: #666;
  font-style: italic;
  border-radius: 0 8px 8px 0;
}

.description-box :deep(strong) {
  font-weight: bold;
  color: #333;
}

.description-box :deep(em) {
  font-style: italic;
}

.description-box :deep(code) {
  background-color: #f1f3f4;
  padding: 2px 6px;
  border-radius: 4px;
  font-family: 'Courier New', monospace;
  font-size: 0.9em;
  color: #e74c3c;
}

.description-box :deep(pre) {
  background-color: #f8f9fa;
  padding: 12px;
  border-radius: 8px;
  overflow-x: auto;
  margin: 12px 0;
  border: 1px solid #e9ecef;
}

.description-box :deep(pre code) {
  background-color: transparent;
  padding: 0;
  color: #333;
}

.description-box :deep(a) {
  color: #007bff;
  text-decoration: none;
}

.description-box :deep(a:hover) {
  text-decoration: underline;
}

.price-section {
  text-align: center;
  margin-top: 24px;
}

.price {
  color: #ff5722;
  font-size: 26px;
  font-weight: bold;
}

.loading-indicator, .error-indicator {
  text-align: center;
  padding: 60px;
  font-size: 20px;
  color: #888;
}

/* 移动端适配 */
@media (max-width: 768px) {
  .script-detail-page {
    padding: 28px 22px;
    font-size: 20px;
  }
  
  .detail-header {
    margin-bottom: 32px;
  }
  
  .back-button {
    font-size: 24px;
    padding: 12px 16px;
    border-radius: 12px;
  }
  
  .detail-header h1 {
    font-size: 24px;
  }
  
  .cover-image-large {
    max-width: 340px;
    margin-bottom: 32px;
    border-radius: 16px;
    box-shadow: 0 8px 20px rgba(0, 0, 0, 0.2);
  }
  
  .script-title {
    font-size: 28px;
    margin-bottom: 26px;
  }
  
  .info-pills, .tag-pills {
    gap: 16px;
    margin-bottom: 26px;
  }
  
  .pill, .tag-pill {
    padding: 12px 20px;
    font-size: 18px;
    min-height: 52px;
    border-radius: 22px;
  }
  
  .description-box {
    padding: 28px;
    font-size: 19px;
    border-radius: 16px;
  }
  
  .description-box h3 {
    font-size: 22px;
    padding-bottom: 16px;
    margin-bottom: 16px;
  }
  
  .description-box :deep(img) {
    max-width: 100% !important;
    width: 100% !important;
    height: auto !important;
    border-radius: 14px;
    box-shadow: 0 6px 16px rgba(0, 0, 0, 0.18);
    margin: 20px 0;
    display: block;
  }
  
  .description-box :deep(p) {
    margin: 0 0 20px 0;
    line-height: 1.8;
    word-wrap: break-word;
    overflow-wrap: break-word;
  }
  
  .description-box :deep(h1) { font-size: 24px; }
  .description-box :deep(h2) { font-size: 22px; }
  .description-box :deep(h3) { font-size: 20px; }
  .description-box :deep(h4) { font-size: 19px; }
  .description-box :deep(h5) { font-size: 18px; }
  .description-box :deep(h6) { font-size: 17px; }
  
  .description-box :deep(ul),
  .description-box :deep(ol) {
    margin: 18px 0;
    padding-left: 28px;
  }
  
  .description-box :deep(li) {
    margin: 10px 0;
    line-height: 1.8;
  }
  
  .description-box :deep(blockquote) {
    margin: 20px 0;
    padding: 18px 22px;
    border-left: 4px solid #ffc107;
    background-color: #f8f9fa;
    border-radius: 0 14px 14px 0;
  }
  
  .description-box :deep(pre) {
    padding: 18px;
    border-radius: 14px;
    margin: 18px 0;
    font-size: 17px;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
  }
  
  .price {
    font-size: 32px;
  }
  
  .loading-indicator, .error-indicator {
    padding: 100px;
    font-size: 24px;
  }
}

/* 超小屏幕适配 */
@media (max-width: 480px) {
  .script-detail-page {
    padding: 24px 18px;
    font-size: 18px;
  }
  
  .detail-header {
    margin-bottom: 28px;
  }
  
  .back-button {
    font-size: 22px;
    padding: 10px 14px;
    border-radius: 10px;
  }
  
  .detail-header h1 {
    font-size: 22px;
  }
  
  .cover-image-large {
    max-width: 300px;
    margin-bottom: 28px;
    border-radius: 14px;
    box-shadow: 0 6px 16px rgba(0, 0, 0, 0.18);
  }
  
  .script-title {
    font-size: 24px;
    margin-bottom: 22px;
  }
  
  .info-pills, .tag-pills {
    gap: 12px;
    margin-bottom: 22px;
  }
  
  .pill, .tag-pill {
    padding: 10px 16px;
    font-size: 16px;
    min-height: 46px;
    border-radius: 20px;
  }
  
  .description-box {
    padding: 24px;
    font-size: 18px;
    border-radius: 14px;
  }
  
  .description-box h3 {
    font-size: 20px;
    padding-bottom: 14px;
    margin-bottom: 14px;
  }
  
  .description-box :deep(img) {
    max-width: 100% !important;
    width: 100% !important;
    height: auto !important;
    border-radius: 12px;
    margin: 18px 0;
  }
  
  .description-box :deep(p) {
    margin: 0 0 18px 0;
    line-height: 1.7;
  }
  
  .description-box :deep(h1) { font-size: 22px; }
  .description-box :deep(h2) { font-size: 20px; }
  .description-box :deep(h3) { font-size: 18px; }
  .description-box :deep(h4) { font-size: 17px; }
  .description-box :deep(h5) { font-size: 16px; }
  .description-box :deep(h6) { font-size: 15px; }
  
  .description-box :deep(ul),
  .description-box :deep(ol) {
    margin: 16px 0;
    padding-left: 24px;
  }
  
  .description-box :deep(li) {
    margin: 8px 0;
    line-height: 1.7;
  }
  
  .description-box :deep(blockquote) {
    margin: 18px 0;
    padding: 16px 20px;
  }
  
  .description-box :deep(pre) {
    padding: 16px;
    font-size: 16px;
  }
  
  .price {
    font-size: 28px;
  }
  
  .loading-indicator, .error-indicator {
    padding: 80px;
    font-size: 20px;
  }
}
</style> 